//
//  MyCardViewController.m
//  SinaWebo
//
//  Created by wwj on 16/5/9.
//  Copyright © 2016年 亦心. All rights reserved.
//

#import "MyCardViewController.h"
#import "UIImage+Extension.h"
#import "UserPerfenceUtil.h"
#import <UIImageView+WebCache.h>

@interface MyCardViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *baImg;
@property (weak, nonatomic) IBOutlet UIImageView *iconImg;
@end

@implementation MyCardViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"我的名片";
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[NSForegroundColorAttributeName] = [UIColor blackColor];
    [self.navigationController.navigationBar setTitleTextAttributes:dict];
    
    [self createQRImage];
}

- (void)createQRImage{
    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    [filter setDefaults];
    NSString *dataStr = @"王亦心";
    [filter setValue:[dataStr dataUsingEncoding:NSUTF8StringEncoding] forKey:@"inputMessage"];
    __weak typeof(self) weakSelf = self;
    [self.iconImg sd_setImageWithURL:[NSURL URLWithString:[UserPerfenceUtil account].avatar_hd] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        UIImage *bgImg = [UIImage createHDImageFormCIImage:filter.outputImage withSize:200.0];
        weakSelf.baImg.image =  [weakSelf createNewImage:bgImg iconImg:image];
    }];
}

- (UIImage *)createNewImage:(UIImage *)bgImage iconImg:(UIImage *)iconImg{
    UIGraphicsBeginImageContextWithOptions(bgImage.size, NO, 0.0);
    CGFloat bW = bgImage.size.width;
    CGFloat bH = bW;
    CGFloat iW = 35;
    CGFloat iH = 35;
    
    [bgImage drawInRect:CGRectMake(0, 0, bW, bH)];
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    [[UIColor greenColor] set];
    
    CGContextAddArc(context, bW * 0.5, bH * 0.5, iW * 0.65, 0, M_PI * 2, 0);
    CGContextClip(context);    
    
    [iconImg drawInRect:CGRectMake((bW - iW) * 0.5, (bH - iH) * 0.5, iW, iH)];
    
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    
    UIGraphicsEndImageContext();
    return newImage;
}


@end
